function gadgetLoad(){
  $('#spinner').hide();
}

function findContacts(){
  alert("find contacts for:" + $('#mail').val());
  $('#spinner').fadeIn('slow');
  fetchData();
}

function fetchData() {
  var mail = $('#mail').val();
  if(wrongMail(mail)){
    return;
  }
  var params = {};
  url = "http://www.google.com/m8/feeds/contacts/"+ mail.replace('@','%40') +"/full?alt=json";
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
  params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
  params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "google";
  params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
  params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;

  gadgets.io.makeRequest(url, function (response) { 
    if (response.oauthApprovalUrl) {
    
      var popup = shindig.oauth.popup({
        destination: response.oauthApprovalUrl,
        windowOptions: null,
        onOpen: function() { showOneSection('waiting'); },
        onClose: function() { fetchData(); }
      });

      var pop = popup.createOpenerOnClick();
      pop();
      //personalize.onclick = 
      //var approvaldone = document.getElementById('approvaldone');
      //approvaldone.onclick = popup.createApprovedOnClick();
      //showOneSection('approval');
    } else if (response.data) {
      var data = response.data;
      
    } else {
      // The response.oauthError and response.oauthErrorText values may help debug
      // problems with your gadget.
      //var main = document.getElementById('main');
      //var err = document.createTextNode('OAuth error: ' +
        //response.oauthError + ': ' + response.oauthErrorText);
      //main.appendChild(err);
      //showOneSection('main');
    }
  }, params);
}

function wrongMail(mail){
  return false;
}

gadgets.util.registerOnLoadHandler(gadgetLoad);